/*
 * HttpMediaCasterConnection.h
 *
 *  Created on: 2019年3月1日
 *      Author: terry
 */

#ifndef HttpMediaCasterConnection_H_
#define HttpMediaCasterConnection_H_

#include "TcpConnection.h"
#include "MediaStream.h"
#include "TByteBuffer.h"
#include "MediaPacketQueue.h"

#include "HttpMessageParser.h"


namespace av
{

class HttpMediaCasterConnection: public TcpConnection ,
                                public av::MediaSink,
                                public std::enable_shared_from_this< HttpMediaCasterConnection >
{
public:
    HttpMediaCasterConnection();
    virtual ~HttpMediaCasterConnection();

    virtual int onOpen();

    virtual void onClose();

    virtual void onWrite(struct bufferevent *bev);

    virtual void onRead(struct bufferevent *bev);

    virtual void onEvent(struct bufferevent *bev, short events);


    virtual void onMediaFormat(const av::MediaFormat& fmt);

    virtual void onMediaPacket(av::MediaPacketPtr& pkt);

    virtual void onMediaEvent(int event);

    void setStreamName(const std::string& name);

protected:
    bool canStream();

    int writeMedia(av::MediaPacketPtr& pkt);

    void handleRecv(struct bufferevent *bev, HttpMessage& req, std::string& buf);

protected:
    std::string m_peerID;
    std::string m_streamName;
    int m_streamState;

    av::MediaFormat m_format;

    bool    m_jamming;  /// 是否拥塞
    size_t  m_maxCacheSize;

    MediaPacketQueue    m_pktQueue;
    bool    m_empty;

    comn::ByteBuffer    m_recvBuffer;
    HttpMessageParser   m_httpParser;

};


} /* namespace av */

#endif /* HttpMediaCasterConnection_H_ */
